package com.pimee.support.manager.factory;

import java.util.TimerTask;

import com.pimee.common.util.SpringContextUtil;
import com.pimee.common.util.net.AddressUtils;
import com.pimee.model.SysUserOnline;
import com.pimee.service.admin.impl.SysUserOnlineService;
import com.pimee.support.shiro.session.OnlineSession;

/**
 * 异步工厂（产生任务用）
 * 
 * @author Bruce Shaw 2020年2月3日 下午5:15:52
 *
 */
public class AsyncFactory {

	/**
	 * 同步session到数据库
	 * 
	 * @param session
	 *            在线用户会话
	 * @return 任务task
	 */
	public static TimerTask syncSessionToDb(final OnlineSession session) {
		return new TimerTask() {
			@Override
			public void run() {
				SysUserOnline online = new SysUserOnline();
				online.setSessionId(String.valueOf(session.getId()));
				online.setLoginName(session.getLoginName());
				online.setCreateTime(session.getStartTimestamp());
				online.setLastAccessTime(session.getLastAccessTime());
				online.setExpireTime(session.getTimeout());
				online.setIp(session.getHost());
				online.setLoginLocation(AddressUtils.getRealAddressByIP(session.getHost()));
				online.setBrowser(session.getBrowser());
				online.setOs(session.getOs());
				online.setStatus(session.getStatus().getCode());
				SpringContextUtil.getBean(SysUserOnlineService.class).saveOnline(online);

			}
		};
	}

}
